package com.mashibing.tank.chainofresponsibility;

import com.mashibing.tank.AbstractGameObject;
import com.mashibing.tank.Bullet;
import com.mashibing.tank.Tank;

import java.awt.*;

/**
 * @description:
 * @author: 余希瑶
 * @date: 2021年06月22日 17:31
 * @version:1.0
 */
public class BulletTankCollider implements Collider {
    @Override
    public boolean collide(AbstractGameObject go1, AbstractGameObject go2) {
        if (go1 instanceof Bullet && go2 instanceof Tank) {
            Bullet b = (Bullet) go1;
            Tank t = (Tank) go2;

            //没活着
            if (!b.isLive() || !t.isLive()) {
                return false;
            }
            //同阵营
            if (b.getGroup() == t.getGroup()) {
                return true;
            }
            Rectangle rectTank = t.getRect();

            if (b.getRect().intersects(rectTank)) {
                b.die();
                t.die();
                return false;
            }

        } else if (go1 instanceof Tank && go2 instanceof Bullet) {
            return collide(go2, go1);
        }

        return true;
    }
}
